package br.ufc.si.sd.proxy;


import br.ufc.si.sd.conexao.Conexao;
import com.google.protobuf.ByteString;
import com.google.protobuf.InvalidProtocolBufferException;


/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
import java.util.HashMap;
import java.util.List;
import protos.br.ufc.si.sd.gerado.AgendaEletronica.AddressBook;
import protos.br.ufc.si.sd.gerado.AgendaEletronica.Person;
import protos.br.ufc.si.sd.gerado.Message.RequestMessage;
import protos.br.ufc.si.sd.gerado.Message.RequestMessage.Builder;
import protos.br.ufc.si.sd.gerado.Message.ResponseMessage;

/**
 *
 * @author Usuario
 */
public class AgendaProxy {

    private RequestMessage.Builder request;
    private ResponseMessage response;
    private Conexao tcp;
    private HashMap<RequestMessage.Builder, ResponseMessage> cache;
   
    public AgendaProxy() {
        tcp= new  Conexao();
        cache = new HashMap<RequestMessage.Builder, ResponseMessage>();
    }

    public String addPerson(RequestMessage.Builder p) throws InvalidProtocolBufferException {
        if (cache.containsKey(p)) {
            response = cache.get(p);
            return response.getMensagem();
        } else {
            byte[] data = tcp.sendMessage(p.build().toByteArray());
            response = ResponseMessage.parseFrom(data);
            cache.put(p, response);
            return response.getMensagem();
        }
    }

    @SuppressWarnings("element-type-mismatch")
    public String removerPerson(Person p) throws InvalidProtocolBufferException {
        request = RequestMessage.newBuilder();
        request.setMetodo("removePerson");
        request.setArgs(p.toByteString());
        if (cache.containsKey(p)) {
            response = cache.get(p);
            return response.getMensagem();
        } else {
            byte[] data = tcp.sendMessage(request.build().toByteArray());
            response = ResponseMessage.parseFrom(data);
            cache.put(request, response);
            return response.getMensagem();
        }
    }

    @SuppressWarnings("element-type-mismatch")
    public String editarPerson(Person p) throws InvalidProtocolBufferException {
        request = RequestMessage.newBuilder();
        request.setMetodo("updatePerson");
        request.setArgs(p.toByteString());
        if (cache.containsKey(p)) {
            response = cache.get(p);
            return response.getMensagem();
        } else {
            byte[] data = tcp.sendMessage(request.build().toByteArray());
            response = ResponseMessage.parseFrom(data);
            cache.put(request, response);
            return response.getMensagem();
        }
    }

    public List<Person> listarPerson() throws InvalidProtocolBufferException {
        request = RequestMessage.newBuilder();
        request.setMetodo("listPeople");
        byte[] data = tcp.sendMessage(request.build().toByteArray());
        response = ResponseMessage.parseFrom(data);
        if (!response.getResult().isEmpty()) {
            cache.put(request, response);
            return AddressBook.parseFrom(response.getResult().toByteArray()).getPersonList();
        } else {
            cache.put(request, response);
            return null;
        }
    }

    @SuppressWarnings("element-type-mismatch")
    public Person buscaPerson(Integer p) throws InvalidProtocolBufferException {
        request = RequestMessage.newBuilder();
        request.setMetodo("getPerson");
        request.setArgs(ByteString.copyFromUtf8(p.toString()));
        if (cache.containsKey(p)) {
            response = cache.get(p);
            Person person = null;
            if (!response.getResult().isEmpty()) {
                person = Person.parseFrom(response.getResult().toByteArray());
            }
            return person;
        } else {
            byte[] data = tcp.sendMessage(request.build().toByteArray());
            response = ResponseMessage.parseFrom(data);
            Person person = null;
            if (!response.getResult().isEmpty()) {
                person = Person.parseFrom(response.getResult().toByteArray());
            }
            cache.put(request, response);
            return person;
        }
    }

   
    public String registrar(RequestMessage.Builder p) throws InvalidProtocolBufferException {
        if (cache.containsKey(p)) {
            response = cache.get(p);
            return response.getMensagem();
        } else {
            byte[] data = tcp.sendMessage(p.build().toByteArray());
            response = ResponseMessage.parseFrom(data);
            cache.put(p, response);
            return response.getMensagem();
        }
    }

}
